﻿Imports System.Object
Imports VSModel
Imports System.Data
Imports Cls_Common
Partial Class FrontEnd_Control_Product_Detail
    Inherits System.Web.UI.UserControl
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            If Not Page.RouteData.Values("CateId").ToString.Equals("0") Then
                hidCateId.Value = Page.RouteData.Values("CateId")
                hidID.Value = Page.RouteData.Values("ProductId")
                ShowData()
            End If
        End If
    End Sub
    'lay nhanh goc cua category theo catid truyen vao
    Protected Function getRootMenu(ByVal _catId As Integer) As Integer
        Dim _hientaiMenuId = _catId
        Using Data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p = (From q In Data.Categories Where q.Domain = DomainName And q.CatId = _hientaiMenuId Select q).FirstOrDefault
            If p Is Nothing Or p.ParentId = 0 Then
                Return _hientaiMenuId
            Else
                Return getRootMenu(p.ParentId)
            End If
        End Using
        Return _hientaiMenuId
    End Function
    Protected Sub ShowData()
        Using data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p As Product = (From q In data.Products Where q.Domain = DomainName And q.ProductId = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                lblTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                lblDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                imgImagePreview.ImageUrl = IIf(IsNothing(p.ImagePreview) = True, "", p.ImagePreview)


                Dim GiaSell = IIf(IsNothing(p.SellPrice) = True, "", FormatNumber(p.SellPrice))

                Page.Title = p.Title
                Page.MetaDescription = p.Description

                'lblSellPrice.Text = GiaSell
                'Dim GiaGoc = IIf(IsNothing(p.OriginalPrice) = True, "", FormatNumber(p.OriginalPrice))
                'If GiaSell = GiaGoc Then
                '    lblOriginalprice.Text = ""
                'Else
                '    lblOriginalprice.Text = GiaGoc
                'End If
                ''lay thong tin nhan hieu
                'Dim _nhanhieu = (From q In data.ProductUnits Where q.ProductUnitID = p.ProductUnitId Select q).FirstOrDefault
                'If Not p.ProductUnitId = "0" Then
                '    lblNhanHieu.Text = _nhanhieu.ProductUnitName
                'Else
                '    lblNhanHieu.Text = ""
                'End If

                ''lay thong tin size
                'Dim _arrSize = (From q In data.ProductSizes Where q.ProductId = p.ProductId Select q.SizeId).ToArray
                'Dim _lstSize = (From q In data.ProSizes Where _arrSize.Contains(q.Id) Select q Order By q.Title).ToList
                'With rptSize
                '    .DataSource = _lstSize
                '    .DataBind()
                'End With
                ''end lay thong tin size
                ''lay thong tin mau
                'Dim _arrColor = (From q In data.ProductColors Where q.ProductId = p.ProductId Select q.ColorId).ToArray
                'Dim _lstColor = (From q In data.ProColors Where _arrColor.Contains(q.Id) Select q Order By q.Title).ToList
                'With rptColor
                '    .DataSource = _lstColor
                '    .DataBind()
                'End With
                ''end lay thong tin smau
                ''lay thong tin mau
                'Dim _arrDoTuoi = (From q In data.ProductDoTuois Where q.ProductId = p.ProductId Select q.DoTuoiId).ToArray
                'Dim _lstDoTuoi = (From q In data.ProDoTuois Where _arrDoTuoi.Contains(q.Id) Select q Order By q.Title).ToList
                'With rptDoTuoi
                '    .DataSource = _lstDoTuoi
                '    .DataBind()
                'End With
                'end lay thong tin smau
                'lay Images
                'Dim _arrAnh = (From q In data.ProductImages Where q.ProductId = p.ProductId Select q.Id).ToArray
                'Dim _lstAnh = (From q In data.ProductImages Where _arrAnh.Contains(q.Id) Select q Order By q.ImageURL).ToList
                'With rptImages
                '    .DataSource = _lstAnh
                '    .DataBind()
                'End With
                ''end lay Images
                ''nhan hieu
                'lblNhanHieuDescription.Text = (From q In data.ProductUnits Where q.ProductUnitID = p.ProductUnitId Select q.Description).FirstOrDefault
            End If
        End Using
    End Sub

#End Region

    'Protected Sub rptSize_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptSize.ItemDataBound
    '    Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
    '    Dim pros = CType(e.Item.DataItem, ProSize)
    '    Dim ltrSize As Literal = CType(e.Item.FindControl("ltrSize"), Literal)
    '    ltrSize.Text = "<div class='Size' ><a href='" & MainDomain & RootCateId.ToString & "/" & "kich-co-" & pros.Id & ".html'>" & pros.Title & "</a></div>"
    'End Sub

    'Protected Sub rptColor_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptColor.ItemDataBound
    '    Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
    '    Dim proc = CType(e.Item.DataItem, ProColor)
    '    Dim ltrColor As Literal = CType(e.Item.FindControl("ltrColor"), Literal)
    '    ltrColor.Text = "<div class='Color' style='background:" & proc.MaMau & "'><a href='" & MainDomain & RootCateId.ToString & "/" & "mau-sac-" & proc.Id & ".html'>" & "&nbsp;</a></div>"
    'End Sub

    'Protected Sub rptDoTuoi_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDoTuoi.ItemDataBound
    '    Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
    '    Dim prodt = CType(e.Item.DataItem, ProDoTuoi)
    '    Dim ltrDoTuoi As Literal = CType(e.Item.FindControl("ltrDoTuoi"), Literal)
    '    ltrDoTuoi.Text = prodt.Title
    '    ltrDoTuoi.Text = "<a href='" & MainDomain & RootCateId.ToString & "/Do-Tuoi-" & prodt.Id & ".html'>" & prodt.Title & "&nbsp;</a>"


    '    'Dim proDoTuoi As New ProDoTuoi
    '    'Dim url As String = MainDomain & RootCateId.ToString & "/Do-Tuoi-" & ddlDoTuoi.SelectedValue & ".html"
    '    'Response.Redirect(url)
    'End Sub

    'Protected Sub rptImages_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImages.ItemDataBound
    '    Dim proi = CType(e.Item.DataItem, Object)
    '    Dim ltrImages As Image = CType(e.Item.FindControl("ltrImages"), Image)
    '    Dim hplImage As HyperLink = CType(e.Item.FindControl("hplImage"), HyperLink)
    '    ltrImages.ImageUrl = CookedImageURL("48-68", proi.ImageURL)
    '    hplImage.Attributes.Add("data-lens-image", proi.ImageURL)
    '    hplImage.Attributes.Add("data-big-image", proi.ImageURL)

    'End Sub

    'Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '    If Not IsPostBack Then
    '        Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
    '        If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
    '            ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
    '        Else
    '            Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
    '        End If


    '        If Not Page.RouteData.Values("ProductId") Is Nothing Then

    '            hidProductID.Value = Page.RouteData.Values("ProductId")
    '            LoadData(hidProductID.Value)
    '        End If

    '    End If
    'End Sub

    'Private Sub LoadData(ByVal intProductId As Integer)
    '    Using Data As New VSEntities
    '        Dim p = (From q In Data.Products Where q.ProductId = intProductId _
    '                  Select q).FirstOrDefault
    '        If Not p Is Nothing Then

    '            Page.Title = p.Title & " | " & DomainName
    '            Page.MetaDescription = p.Description.Replace("\n", " ")

    '            ltrCode.Text = p.Code
    '            ltrTitle.Text = p.Title

    '            ltrPrice.Text = FormatNumber(p.SellPrice.ToString())
    '            If p.IsSellOff = True Then
    '                lblOriginalPrice.Text = FormatNumber(p.OriginalPrice.ToString())
    '            End If


    '            ltrThongTinChung.Text = p.Description

    '            If p.TinhTrangHang = 0 Then
    '                ltrTinhTrang.Text = " còn hàng"
    '            ElseIf p.TinhTrangHang = 1 Then
    '                ltrTinhTrang.Text = " hết hàng"
    '                btnAddCart.Enabled = False
    '            Else
    '                ltrTinhTrang.Text = " ngừng bán"
    '                btnAddCart.Enabled = False
    '            End If
    '            Dim h = (From f In Data.ProductImages Where f.ProductId = p.ProductId Select f.ImageURL, f.Description).ToList
    '            If h.Count > 0 Then
    '                rptBigBanner.DataSource = h
    '                rptBigBanner.DataBind()
    '                rptImageThumb.DataSource = h
    '                rptImageThumb.DataBind()
    '            Else
    '                Dim k = (From q In Data.Products Where q.ProductId = p.ProductId Select New With {.ImageURL = q.ImagePreview, .Description = q.Description}).ToList
    '                rptBigBanner.DataSource = k
    '                rptBigBanner.DataBind()
    '                rptImageThumb.DataSource = k
    '                rptImageThumb.DataBind()
    '            End If



    '            hidCateId.Value = p.CateID

    '            BindToGrid(1)
    '            'bind san pham cung cat
    '            'Dim k = (From q In Data.Products Where q.ProductId <> p.ProductId And q.CateID = q.CateID And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
    '            '             Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(15).ToList
    '            'rptShow.DataSource = k
    '            'rptShow.DataBind()


    '        End If
    '    End Using
    'End Sub

    'Protected Sub rptImageThumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImageThumb.ItemDataBound
    '    Dim row As Object = e.Item.DataItem
    '    Dim imgThumb As Image = e.Item.FindControl("imgThumb")
    '    imgThumb.ImageUrl = CookedImageURL("90-60", row.ImageURL)
    '    imgThumb.AlternateText = row.Description
    '    imgThumb.Style.Add("overflow", "hidden")

    'End Sub

    'Protected Sub rptBigBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBigBanner.ItemDataBound
    '    Dim row As Object = e.Item.DataItem
    '    Dim HighslideImage1 As Encosia.HighslideImage = e.Item.FindControl("HighslideImage1")
    '    HighslideImage1.ImageUrl = CookedImageURL("320-300", row.ImageURL)
    '    HighslideImage1.FullImageURL = CookedImageURL("800-600", row.ImageURL)
    '    HighslideImage1.AlternateText = row.Description
    '    If e.Item.ItemIndex = 0 Then
    '        HighslideImage1.Attributes.Add("style", "display:inline")
    '    Else
    '        HighslideImage1.Attributes.Add("style", "display:none")
    '    End If

    'End Sub


    'Private Sub BindToGrid(ByVal ipage As Integer)
    '    Using Data As New VSEntities

    '        hplCate.Text = (From q In Data.Categories Where q.CatId = hidCateId.Value Select q.Title).FirstOrDefault

    '        Dim a = (From r In Data.uspSelectSubCate(hidCateId.Value) Select r.CateID).ToArray

    '        Dim p = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
    '                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
    '                                            Or q.eEffectiveDate Is Nothing) _
    '                                        Order By q.PublishedDate Descending Select q).Skip((ipage - 1) * 12).Take(12).ToList

    '        Dim count = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
    '                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
    '                                            Or q.eEffectiveDate Is Nothing) _
    '                                        Order By q.PublishedDate Descending Select q.ProductId).Count

    '        If count > 0 Then
    '            hidCount.Value = count
    '            Create_Pager(hidCount.Value, ipage, 12, 10)
    '        Else
    '            hidCount.Value = 0
    '            With rptPage
    '                .DataSource = Nothing
    '                .DataBind()
    '            End With
    '            results_resultsNavigationDiv.Visible = False
    '        End If

    '        With rptProduct
    '            .DataSource = p
    '            .DataBind()
    '        End With

    '    End Using
    'End Sub

    'Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
    '    Dim row = CType(e.Item.DataItem, Product)
    '    Dim img As Image = CType(e.Item.FindControl("img"), Image)
    '    Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
    '    Dim hplTitleImage As HyperLink = CType(e.Item.FindControl("hplTitleImage"), HyperLink)
    '    img.ImageUrl = CookedImageURL("212-180", row.ImagePreview)
    '    img.AlternateText = row.Title
    '    img.Style.Add("overflow", "hidden")
    '    hplTitle.Text = row.Title
    '    hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
    '    hplTitleImage.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
    '    Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
    '    Dim ltrSaleOff As Literal = CType(e.Item.FindControl("ltrSaleOff"), Literal)
    '    If row.IsSellOff = True Then
    '        ltrSaleOff.Text = "<span class=""SellOff"">" & FormatNumber(row.OriginalPrice) & "</span>"
    '    End If

    '    ltrPrice.Text = FormatNumber(row.SellPrice.ToString())
    'End Sub

    'Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
    '    Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
    '    'lu lai tong so ban ghi
    '    hidIndex_page.Value = TotalPage
    '    'gan lai curPage de set active
    '    hidCur_Page.Value = Page_Index
    '    'generate ra left page
    '    Dim cPageGenerate_left As IEnumerable(Of Integer)
    '    If Page_Index <= Page2Show Then
    '        cPageGenerate_left = Enumerable.Range(1, Page_Index)
    '    Else
    '        cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
    '    End If
    '    'generate ra right page
    '    Dim cPageGenerate_Right As IEnumerable(Of Integer)
    '    If Page_Index + Page2Show <= TotalPage Then
    '        cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
    '    Else
    '        cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
    '    End If
    '    'union 2 range va bind to Grid
    '    With rptPage
    '        .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
    '        .DataBind()
    '    End With
    'End Sub

    'Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
    '    Dim lnkTitle As LinkButton
    '    lnkTitle = e.Item.FindControl("lnkTitle")
    '    Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
    '    ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
    '    If e.Item.DataItem = hidCur_Page.Value Then
    '        lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
    '    Else
    '        lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
    '    End If
    '    lnkTitle.ToolTip = e.Item.DataItem
    'End Sub

    'Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Dim lnkTile As LinkButton = CType(sender, LinkButton)
    '    BindToGrid(lnkTile.ToolTip)
    '    lnkLast.Enabled = True
    '    lnkFirst.Enabled = True
    '    If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
    '        lnkLast.Enabled = False
    '    ElseIf CInt(lnkTile.ToolTip) = 1 Then
    '        lnkFirst.Enabled = False
    '    End If
    'End Sub
    'Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
    '    If hidCur_Page.Value > 1 Then
    '        hidCur_Page.Value = hidCur_Page.Value - 1
    '    End If
    '    BindToGrid(hidCur_Page.Value)
    'End Sub
    'Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
    '    hidCur_Page.Value = hidCur_Page.Value + 1
    '    BindToGrid(hidCur_Page.Value)
    'End Sub


    'Protected Sub lblDatHang_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblDatHang.Click

    '    AddShoppingCart(hidID.Value)
    '    Session("LastURL") = Request.Url
    '    Response.Redirect(MainDomain & "gio-hang.html")


    'End Sub

    Private Sub AddShoppingCart(ByVal _ProductID As String)
        Using Data As New VSEntities
            Dim _cartID As String = Session("CartId")
            'Add DomainName DUC 09-09-2014
            Dim check = (From q In Data.ShoppingCarts Where q.Domain = DomainName And q.CartId = _cartID And q.ProductId = _ProductID Select q).FirstOrDefault
            If check Is Nothing Then
                Dim p As New ShoppingCart
                p.CartId = _cartID
                p.ProductId = _ProductID
                p.UserId = Session("UserId")
                ' p.StoreId = (From q In Data.Products Where q.ProductId = _ProductID Select q.StoreId).FirstOrDefault
                p.Quantity = 1
                Data.ShoppingCarts.AddObject(p)
                Data.SaveChanges()

            End If

        End Using


    End Sub


End Class

